package com.rocksdata.mc.ui.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import com.ctz.commonframe.ui.TitleBarActivity;
import com.ctz.commonframe.ui.view.ptr.PullToRefreshBase;
import com.ctz.commonframe.ui.view.ptr.PullToRefreshListView;
import com.ctz.commonframe.ui.widget.AbsAdapter;
import com.ctz.commonframe.ui.widget.AdapterViewHolder;
import com.rocksdata.mc.R;
import com.rocksdata.mc.business.BusinessInterface;
import com.rocksdata.mc.business.BusinessManager;
import com.rocksdata.mc.entity.MyOAData;
import com.ctz.commonframe.ui.BottomMenuDialog;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

@ContentView(R.layout.activity_my_oa_apply)
public class MyOAApplyActivity extends TitleBarActivity {
    @ViewInject(R.id.lv_my_apply)
    private PullToRefreshListView lvMyApply;
    private BottomMenuDialog bottomMenuDialog;

    private List<MyOAData> myOAApplyList = new ArrayList<>();
    private MyOAApplyListAdapter adapter;
    private BusinessInterface.IOAManager manager;
    private int index;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitleMiddleText("我的申请");

        manager = BusinessManager.getOAManager();
        lvMyApply.setMode(PullToRefreshBase.Mode.BOTH);
        adapter = new MyOAApplyListAdapter(this, myOAApplyList);
        lvMyApply.setAdapter(adapter);
        lvMyApply.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                refresh();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                loadMore();
            }
        });
//        lvMyApply.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                MyOAData info = adapter.getItem(position - 1);
//                if(!TextUtils.isEmpty(info.getTaskPage())) {
//                    Intent intent = new Intent(MyOAApplyActivity.this, WebViewActivity.class);
//                    intent.putExtra(WebViewActivity.EXTRA_KEY_URL, info.getTaskPage()
//                            + "&uid=" + UserInfoManager.getCurrentUserId()
//                            + "&pwd=" + UserInfoManager.getOAPassword());
//                    startActivity(intent);
//                }
//            }
//        });
        lvMyApply.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                MyOAData info = adapter.getItem(position - 1);
                showDeleteOAApplyDialog(info);
                return true;
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        showLoadingDialog();
        refresh();
    }

    private void refresh() {
        index = 1;
        myOAApplyList.clear();
        getMyOAApplyList();
    }

    private void loadMore() {
        index++;
        getMyOAApplyList();
    }

    private void getMyOAApplyList() {
        manager.getMyOAApplyList(index, new BusinessInterface.OnBusinessResultListener() {
            @Override
            public void onBusinessResult(boolean isSuccess, Object object, String message) {
                if(isSuccess){
                    @SuppressWarnings("unchecked")
                    List<MyOAData> list = (List<MyOAData>) object;
                    if(list != null && list.size() > 0){
                        for(MyOAData info: list){
                            if(!myOAApplyList.contains(info)){
                                if("客饭申请v2".equals(info.getWorkName())
                                        || "用车申请v2".equals(info.getWorkName())
                                        || "请假申请v2".equals(info.getWorkName())
                                        || "故障报修申请v1".equals(info.getWorkName())
                                        || "出差申請".equals(info.getWorkName())){
                                    myOAApplyList.add(info);
                                }
                            }
                        }
                    }
                }else{
                    if(index > 1)
                        index--;
                }
                adapter.notifyDataSetChanged();
                lvMyApply.onRefreshComplete();
                dismissLoadingDialog();
            }
        });
    }

    private void deleteOAApply(final MyOAData data) {
        showLoadingDialog();
        manager.deleteOAApply(data.getInstanceID(), new BusinessInterface.OnBusinessResultListener() {
            @Override
            public void onBusinessResult(boolean isSuccess, Object object, String message) {
                if (isSuccess) {
                    showToast("删除成功");
                    myOAApplyList.remove(data);
                    adapter.notifyDataSetChanged();
                } else {
                    showToast("删除失败");
                }
                dismissLoadingDialog();
            }
        });
    }

    private void showDeleteOAApplyDialog(final MyOAData data) {
        if (bottomMenuDialog == null) {
            bottomMenuDialog = new BottomMenuDialog(MyOAApplyActivity.this, BottomMenuDialog.TYPE_NORMAL_DELETE);
            bottomMenuDialog.setOnMenu2ClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    bottomMenuDialog.dismiss();
                    deleteOAApply(data);
                }
            });
        }
        bottomMenuDialog.show();
    }

    private class MyOAApplyListAdapter extends AbsAdapter<MyOAData> {

        private MyOAApplyListAdapter(Context context, List<MyOAData> datas) {
            super(context, datas, R.layout.item_oa_apply_list);
        }

        @Override
        public void convert(AdapterViewHolder holder, MyOAData data) {
            TextView tvType = holder.getView(R.id.tv_oa_type);
            TextView tvDate = holder.getView(R.id.tv_propose_date);
            TextView tvResult = holder.getView(R.id.tv_result);

            tvType.setText(data.getWorkName());
            tvDate.setText(data.getApplyDateTime());
            tvResult.setText(data.getStatus());
        }
    }
}
